package czk.concrent.threadpool.exceptionhandle;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 线程池中异常的处理
 * @Author:ChenZhangKun
 * @Date: 2020/7/8 22:45
 */
public class ThreadPoolExceptionHandlerDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(2);
        // 自己主动try catch异常,此时可以打印异常信息
        pool.submit(()->{
           try {
               System.out.println(1);
               int i=1/0;
               System.out.println(i);
           }catch (Exception ex){
               ex.printStackTrace();
           }
        });
        //2 利用calllable来获取异常
        Future<Boolean> f=pool.submit(()->{
            System.out.println(2);
            int i=1/0;
            System.out.println(i);
            return true;
        });
        f.get(); // 此时会打印异常
    }
}
